package cn.rui.controller;

import cn.rui.constant.MessageConstant;
import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.entity.Result;
import cn.rui.pojo.Menu;
import cn.rui.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 * @author 徽州大都督
 * @date 2020/9/2
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;


    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return menuService.findPage(queryPageBean);
    }

    /**
     * 查询主菜单名称集合
     * @return
     */
    @GetMapping("findMainMenuName")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findMainMenuName(){
        try {
            List<String> nameList=menuService.findMainMenuName();
            return new Result (true, MessageConstant.GET_MENU_SUCCESS,nameList);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false, MessageConstant.SYSTEM_ERROR);
        }
    }

    /**
     * 添加菜单项
     * @param map
     * @return
     */
    @PostMapping("handleAdd")
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    public Result handleAdd(@RequestBody Map map){

        try {
            menuService.handleAdd(map);
            return new  Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new  Result(false,MessageConstant.ADD_MENU_FAIL);
        }

    }

    /**
     * 删除菜单项
     */
    @GetMapping("handleDelete/{id}")
    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")
    public Result handleDelete(@PathVariable("id")Integer id){

        try {
            menuService.deleteById(id);
            return new Result (true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 根据id查询菜单信息
     */
    @GetMapping("handleUpdate/{id}")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Map handleUpdate(@PathVariable("id")Integer id){

        return menuService.findById(id);
    }

    /**
     * 主菜单分页查询
     */
    @PostMapping("findMainPage")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public PageResult findMainPage(@RequestBody QueryPageBean queryPageBean){

        return menuService.findMainPage(queryPageBean);
    }

    /**
     * 添加主菜单
     */
    @PostMapping("MainMenuAdd")
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    public Result MainMenuAdd(@RequestBody Menu menu){
        try {
            menuService.MainMenuAdd(menu);
            return new Result (true,MessageConstant.ADD_MAINMENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.ADD_MAINMENU_FAIL);
        }

    }

    /**
     * 编辑菜单项
     */
    @PostMapping("handleEdit")
    @PreAuthorize("hasAnyAuthority('MENU_EDIT')")
    public Result handleEdit(@RequestBody Map map){

        try {
            menuService.handleEdit(map);
            return new Result (true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 编辑主菜单
     */
    @PostMapping("MainMenuEdit")
    @PreAuthorize("hasAnyAuthority('MENU_EDIT')")
    public Result MainMenuEdit(@RequestBody Menu menu){


        try {
            menuService.MainMenuEdit(menu);
            return new Result (true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 查询所有菜单选项
     *
     * @return
     */
    @RequestMapping("/findAllMenuByRole")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public List<Menu> findAllPmsByRole() {
        return menuService.findAllMenByRole();
    }
}
